﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Media.Imaging;
using System.IO;

namespace TinyCinema.Data
{
    public class Promotion : Execution
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public Image Photo { get; set; }
        public ObservableCollection<CustomerType> CustomerTypes { get; set; }
        public Promotion()
        {
            ID = IDGenerate(Tables.Promotion);
            Photo = new Image();
            CustomerTypes = CustomerType.getAllCustomerType();
            StartDate = DateTime.Now;
            EndDate = new DateTime(StartDate.Year, StartDate.Month, StartDate.Day + 5);
        }
        public Promotion(string _ID)
        {
            ID = _ID;
            Photo = new Image();
            PutTableIntoDataSet(Tables.Promotion);
            using (var tbl = ds.Tables[Tables.Promotion])
            {
                var Row = tbl.Select(PromotionTable.ID + "='" + ID + "'")[0];
                Name = Row[PromotionTable.Name] as string;
                Description = Row[PromotionTable.Desciption] as string;
                StartDate = (DateTime)Row[PromotionTable.StartDate];
                EndDate = (DateTime)Row[PromotionTable.EndDate];
                try { Photo = new Image(Row[PromotionTable.Photo] as string); }
                catch { }
                CustomerTypes = CustomerType.getAllCustomerType();
            }
            PutTableIntoDataSet(Tables.PromotionDetail);
            using (var tbl=ds.Tables[Tables.PromotionDetail])
            {
                foreach(var customertype in CustomerTypes)
                {
                    try
                    {
                        var Row = tbl.Select(PromotionDetailTable.PromotionID + "='" + ID + "' AND " + PromotionDetailTable.CustomerTypeID + "='" + customertype.ID + "'")[0];
                        
                        customertype.PromotionDiscount = (double)Row[PromotionDetailTable.PromotionDiscount];
                    }
                    catch { }
                }
            }       
        }
        
        public static ObservableCollection<Promotion> GetAllPromotion()
        {
            PutTableIntoDataSet(Tables.Promotion);
            var tbl = ds.Tables[Tables.Promotion];
            var Rows = tbl.Select();
            var Result = new ObservableCollection<Promotion>();
            foreach (var Row in Rows)
            {
                var promotion = new Promotion(Row[PromotionTable.ID] as string);
                Result.Add(promotion);
            }
            return Result;
        }
        public void Save()
        {
            #region Lưu Khuyến Mãi
            using (var tbl = ds.Tables[Tables.Promotion])
            {
                var Rows = ds.Tables[Tables.Promotion].Select(PromotionTable.ID + "='" + ID + "'");
                if (Rows.Length == 0)
                {
                    var Row = tbl.NewRow();
                    Row[PromotionTable.ID] = ID;
                    Row[PromotionTable.Name] = Name;
                    Row[PromotionTable.Desciption] = Description;
                    Row[PromotionTable.StartDate] = StartDate;
                    Row[PromotionTable.EndDate] = EndDate;
                    Row[PromotionTable.Photo] = Photo.ToBase64String();
                    tbl.Rows.Add(Row);
                    SaveCommand(Tables.Promotion);
                }
                else
                {
                    foreach (System.Data.DataRow Row in tbl.Rows)
                        if ((Row[PromotionTable.ID] as string) == ID)
                        {
                            Row[PromotionTable.ID] = ID;
                            Row[PromotionTable.Name] = Name;
                            Row[PromotionTable.Desciption] = Description;
                            Row[PromotionTable.StartDate] = StartDate;
                            Row[PromotionTable.EndDate] = EndDate;
                            Row[PromotionTable.Photo] = Photo.ToBase64String();
                            SaveCommand(Tables.Promotion);
                        }
                }
            }
            #endregion
            #region Lưu Chi Tiết Khuyến Mãi
            using (var tbl =ds.Tables[Tables.PromotionDetail])
            {
                PutTableIntoDataSet(Tables.PromotionDetail);
                foreach(var customertype in CustomerTypes)
                {
                    var Rows = tbl.Select(PromotionDetailTable.PromotionID + "='" + ID + "' AND " + PromotionDetailTable.CustomerTypeID + "='" + customertype.ID + "'");
                    if(Rows.Length==0)
                    {
                        var Row = tbl.NewRow();
                        Row[PromotionDetailTable.PromotionID] = ID;
                        Row[PromotionDetailTable.CustomerTypeID] = customertype.ID;
                        Row[PromotionDetailTable.PromotionDiscount] = customertype.PromotionDiscount;
                        tbl.Rows.Add(Row);
                        SaveCommand(Tables.PromotionDetail);
                    }
                    else
                    {
                        var Row = Rows[0];
                        Row[PromotionDetailTable.PromotionID] = ID;
                        Row[PromotionDetailTable.CustomerTypeID] = customertype.ID;
                        Row[PromotionDetailTable.PromotionDiscount] = customertype.PromotionDiscount;
                        SaveCommand(Tables.PromotionDetail);
                    }
                }
            }
            #endregion
            
        }
        public void AddPhoto()
        {
            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.Filter = "Image|*.jpg";
            if (dialog.ShowDialog().Value)
            {
                MemoryStream memoryStream = new MemoryStream();
                BitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(new BitmapImage(new Uri(dialog.FileName))));
                encoder.Save(memoryStream);
                var bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.StreamSource = memoryStream;
                bitmapImage.EndInit();
                Photo.DisplayImage = bitmapImage;
            }
        }
        public void Delete()
        {
            using (var tbl = ds.Tables[Tables.PromotionDetail])
            {
                string Condition = PromotionDetailTable.PromotionID + "='" + ID + "'";
                foreach (var Row in tbl.Select(Condition))
                    Row.Delete();
                SaveCommand(Tables.PromotionDetail);
            }
            using (var tbl = ds.Tables[Tables.Promotion])
            {
                string Condition = PromotionTable.ID + "='" + ID + "'";
                tbl.Select(Condition)[0].Delete();
                SaveCommand(Tables.Promotion);
            }
        }
        public static ObservableCollection<Promotion> SearchByName(string str)
        {
            PutTableIntoDataSet(Tables.Promotion);
            var tbl = ds.Tables[Tables.Promotion];
            var Rows = tbl.Select(PromotionTable.Name + " Like '%" + str + "%'");
            var Result = new ObservableCollection<Promotion>();
            foreach (var Row in Rows)
            {
                Result.Add(new Promotion(Row[PromotionTable.ID] as string));
            }

            return Result;
        }

        public static ObservableCollection<Promotion> SearchByNo(string str)
        {
            PutTableIntoDataSet(Tables.Promotion);
            var tbl = ds.Tables[Tables.Promotion];
            var Rows = tbl.Select(PromotionTable.ID + " Like '%" + str + "%'");
            var Result = new ObservableCollection<Promotion>();
            foreach (var Row in Rows)
            {
                Result.Add(new Promotion(Row[PromotionTable.ID] as string));
            }
            return Result;
        }
    }
    
}
